package de.lehmann.sudoku.userInterface.graphical;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ComponentListener;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class SGrid extends JPanel {
	/**
	 * The space between this Grids components.
	 */
	public static final int OFFSET = 2;
	private static SGrid active;
	/**
	 * This method is used by sub-components of SGrid,
	 * to know which SGrid they belong to without passing it around.
	 * @return The SGrid that is currently being created.
	 */
	protected static SGrid active(){
		return active;
	}
	private static final long serialVersionUID = 8414883702436809672L;
	
	/**
	 * The width of the SBoxes contained by this Grid.
	 */
	public final int WIDTH;
	/**
	 * The height of the SBoxes contained by this Grid.
	 */
	public final int HEIGHT;
	public final int SIZE;
	private ComponentListener resizer;
	private SBox[][] boxes;

	/**
	 * Creates a new Grid that will be <b>width*height</b> STiles in both width and height.
	 * 
	 * @param width - The number of STiles every of the contained SBoxes should be wide.
	 * @param height - The number of STiles every of the contained SBoxes should be high.
	 */
	public SGrid(int width, int height) {
		WIDTH = width;
		HEIGHT = height;
		SIZE = WIDTH*HEIGHT;
		setBackground(Color.DARK_GRAY);
		setLayout(new GridLayout(0, HEIGHT, OFFSET, OFFSET));
		setBorder(new LineBorder(Color.BLACK, OFFSET+1));
		resizer = new Resizer();
		
		active = this; // Set active to this SGrid, for easy access is sub-components.
		
		boxes = new SBox[HEIGHT][WIDTH];
		for(int y = 0; y < WIDTH; y++){
			for(int x = 0; x < HEIGHT; x++){
				boxes[x][y] = new SBox(x, y);
				add(boxes[x][y]);
			}
		}
		
		addComponentListener(resizer);
		active = null; // Set active back to null so no new sub-components can access this SGrids' data.
	}
	
	protected void adaptFont() {
		for(int y = 0; y < WIDTH; y++){
			for(int x = 0; x < HEIGHT; x++){
				boxes[x][y].adaptFont();
			}
		}
	}

}
